package com.th.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.th.bean.Category;
import com.th.bean.Dish;
import com.th.bean.Setmeal;
import com.th.common.CustomerException;
import com.th.mapper.CategoryMapper;
import com.th.service.CategoryService;
import com.th.service.DishService;
import com.th.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServicelmpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dlw = new LambdaQueryWrapper<>();
        dlw.eq(Dish::getCategoryId,id);

        int count1 = dishService.count(dlw);
        if(count1>0){
            throw  new CustomerException("已经关联菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> slw = new LambdaQueryWrapper<>();
        slw.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(slw);
        if(count2>0){
            throw  new CustomerException("已经关联套餐，不能删除");
        }

        super.removeById(id);
    }
}


